package com.youdianstar.app.ui

import com.gyf.immersionbar.ImmersionBar
import com.youdianstar.app.common.Language
import com.youdianstar.app.common.auko.startActivity
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.entity.LanguageEntity
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.util.LanguageConfig
import com.youdianstar.app.common.util.StrUtil
import com.youdianstar.app.R
import com.youdianstar.app.BR
import com.youdianstar.app.databinding.ActivitySplashGuideBinding
import com.youdianstar.app.ui.account.LoginActivity
import com.youdianstar.app.util.AppDialogUtil
import com.youdianstar.app.util.DataAppManager
import org.koin.androidx.viewmodel.ext.android.viewModel
import java.util.Locale

/**
 * File: GuideActivity
 * Author: Administrator Create: 2025/2/13 0013 14:23
 * Changes (from 2025/2/13 0013)
 * --------------------------------------------------
 * description: 引导页
 * ---------------------------------------------------
 *
 */
class GuideActivity : BaseActivity<ActivitySplashGuideBinding, SplashViewModel>() {
    override fun layoutId(): Int = R.layout.activity_splash_guide

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): SplashViewModel {
        val model: SplashViewModel by viewModel()
        return model
    }

    private var selectCode: String = ""
    private var selectIndex:Int = 0
    private var languageList : List<LanguageEntity>?=null

    override fun initData() {
        super.initData()
        ImmersionBar.with(this)
            .titleBarMarginTop(mBinding.llBar)
            .statusBarColor(R.color.color_EB0045)
            .statusBarDarkFont(false)
            .init()

        initLanguage()
        DataAppManager.instance.setShowGuideImg(true)
    }

    override fun initClickEvent() {
        mBinding.llSwitchTop.setThrottleListener {
            showSwitchDialog()
        }

        mBinding.btnGo.setThrottleListener {
            if (DataAppManager.instance.isTokenExist()) {
                startActivity<MainActivity>()
            } else {
                startActivity<LoginActivity>()
            }
        }
    }

    private fun initLanguage(){
        languageList = listOf(
            LanguageEntity(Language.VI, getString(R.string.Set_language_VI)),
            LanguageEntity(Language.ZH, getString(R.string.Set_language_CH)),
            LanguageEntity(Language.EN, getString(R.string.Set_language_EN))
            // LanguageEntity(Language.ES, getString(R.string.Set_language_ES)),
        )
        val localLan = LanguageConfig.instance.getLanguageCode()
        for ((index,lan) in languageList!!.withIndex()){
            if (lan.languageCode.lowercase(Locale.ROOT) == localLan){
                selectIndex = index
                mBinding.tvSwitch.text = lan.language
            }
        }
    }

    private fun showSwitchDialog() {
        val list = mutableListOf<String>()
        languageList?.let { lanList ->
            for ((id,item) in lanList.withIndex()){
                list.add(item.language)
            }
        }
        AppDialogUtil.instance.showBoxSelectDialog(this@GuideActivity, getString(R.string.set_selectLanguage), list,defaultP = selectIndex) {
            selectIndex = it
            languageList?.let { list->
                selectCode = list[selectIndex].languageCode
                if (!StrUtil.isEmpty(selectCode)){
                    LanguageConfig.instance.saveLanguageCode(selectCode)

                    recreate()
                }
            }
        }
    }
}